// 测试Buffer polyfill
import { Buffer } from 'buffer';
import { authenticator } from 'otplib';

// 设置全局Buffer
if (typeof globalThis !== 'undefined') {
  globalThis.Buffer = Buffer;
}

const MY_SECRET = 'vuixppgt3kx3fbtjp2pzgfzzccxqkv7q';

console.log('=== 测试Buffer polyfill ===');
console.log('Buffer是否可用:', typeof Buffer !== 'undefined');
console.log('globalThis.Buffer是否可用:', typeof globalThis.Buffer !== 'undefined');

function testBufferPolyfill() {
  try {
    // 测试Buffer功能
    const testBuffer = Buffer.from('test', 'utf8');
    console.log('Buffer测试:', testBuffer.toString());
    
    // 测试otplib
    const code = authenticator.generate(MY_SECRET);
    console.log('生成的TOTP:', code);
    
    const remaining = authenticator.timeRemaining();
    console.log('剩余时间:', remaining, '秒');
    
    console.log('\n=== Buffer polyfill测试成功！===');
    
  } catch (error) {
    console.error('测试失败:', error);
  }
}

testBufferPolyfill();
